
import SQLite

class DbRequest : DbBase {
    var request: Table = Table("request")
    let _id = Expression<Int64>("id")
    let _type = Expression<Int>("type")
    let _direction = Expression<Int>("direction")
    let _from = Expression<String>("from")
    let _to = Expression<String>("to")
    let _group = Expression<String>("group")
    let _msgHash = Expression<String>("msgHash")
    let _reason = Expression<String>("reason")
    let _recover = Expression<String>("recover")
    let _status = Expression<Int>("status")
    let _timestamp = Expression<Int64>("timestamp")
    let _from_to = Expression<String>("_from_to")
    
    func open(){
        do{
            try getDb()!.run(request.create(ifNotExists: true) { t in
                t.column(_id, primaryKey: .autoincrement)
                t.column(_type)
                t.column(_direction)
                t.column(_from)
                t.column(_to)
                t.column(_group)
                t.column(_msgHash, unique: true)
                t.column(_reason)
                t.column(_recover)
                t.column(_status)
                t.column(_timestamp)
                t.column(_from_to, unique: true)
            })
            try getDb()!.run(request.createIndex(_msgHash, ifNotExists: true))
            try getDb()!.run(request.createIndex(_from, _to, ifNotExists: true))

        }catch{
            print("\(error)")
        }
    }
    func toInfo(_ row:Row)->DMCCRequest{
        let u = DMCCRequest()
        u.id = row[_id]
        u.type = OsnRequestType(rawValue: row[_type])
        u.direction = OsnRequestDir(rawValue: row[_direction])
        u.from = row[_from]
        u.to = row[_to]
        u.group = row[_group]
        u.reason = row[_reason]
        u.recover = row[_recover]
        u.hash = row[_msgHash]
        u.status = OsnRequestStatus(rawValue: row[_status])
        u.timestamp = row[_timestamp]
        return u
    }
    func getRequestInfo(_ osnId:String, _ friendId:String) -> DMCCRequest? {
        do{
            for req in try getDb()!.prepare(request.filter(osnId == _to && friendId == _from)) {
                return toInfo(req)
            }
        }catch{
            print("\(error)")
        }
        return nil
    }
    func addRequestInfo(_ info:DMCCRequest) -> Bool {
        do{
            return try getDb()!.run(request.upsert(
                _type <- info.type.rawValue,
                _direction <- info.direction.rawValue,
                _from <- info.from,
                _to <- info.to,
                _group <- info.group,
                _msgHash <- info.hash,
                _reason <- info.reason,
                _recover <- info.recover,
                _status <- info.status.rawValue,
                _timestamp <- info.timestamp,
                _from_to <- info.from+info.to,
                onConflictOf: _from_to)) >= 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func listRequestSend()->[DMCCRequest]{
        var rs:[DMCCRequest] = []
        do{
            for req in try getDb()!.prepare(request.filter(gimService.mOsnId != _from)) {
                let info = toInfo(req)
                rs.append(info)
            }
        }catch{
            print("\(error)")
        }
        return rs
    }
    func listRequestReceive()->[DMCCRequest]{
        var rs:[DMCCRequest] = []
        do{
            for req in try getDb()!.prepare(request.filter(gimService.mOsnId != _from)) {
                let info = toInfo(req)
                rs.append(info)
            }
        }catch{
            print("\(error)")
        }
        return rs
    }
    
    
//    func updateMessageInfo(_ hash:String, _ updater: Setter) -> Bool {
//        do{
//            return try getDb()!.run(message.filter(_msgHash == hash).update(updater)) >= 0
//
//        }catch{
//            print("\(error)")
//        }
//        return false
//    }
    

    
    func clearUnread(){ //1.fw
        
        do{
//            var list:[DMCCFriend] = []
            for friend in try getDb()!.prepare(request) {
//                _status = Int(1)
                
//                list.append(toInfo(friend))
            }
        }catch{
            print("\(error)")
        }
        
        
//        var rs:[DMCCRequest] = []
//        do{
//            for req in try getDb()!.prepare(request.filter(gimService.mOsnId != _from)) {
//                let info = toInfo(req)
//                rs.append(info)
//            }
//        }catch{
//            print("\(error)")
//        }
//        return rs
    }
}
let dbRequest = DbRequest()
